package com.lovecws.mumu.ffmpeg.thumb;

import com.lovecws.mumu.ffmpeg.video.VideoInfo;
import com.lovecws.mumu.ffmpeg.video.VideoInfoService;

import java.io.IOException;

/**
 * Created by Administrator on 2017/5/17.
 */
public class VideoThumbTakerService {

    private String ffmpegApp;

    private static VideoFirstThumbTaker videoFirstThumbTaker = null;
    private static VideoLastThumbTaker videoLastThumbTaker=null;
    private static VideoThumbTaker videoThumbTaker=null;

    public VideoThumbTakerService(String ffmpegApp) {
        this.ffmpegApp = ffmpegApp;
        videoFirstThumbTaker = new VideoFirstThumbTaker(ffmpegApp);
        videoLastThumbTaker=new VideoLastThumbTaker(ffmpegApp);
        videoThumbTaker=new VideoThumbTaker(ffmpegApp);
    }

    /****
     * 获取指定时间内的图片
     * @param videoFilename:视频路径
     * @param thumbFilename:图片保存路径
     * @param width:图片长
     * @param height:图片宽
     * @param hour:指定时
     * @param min:指定分
     * @param sec:指定秒
     * @throws IOException
     * @throws InterruptedException
     */
    public void getThumb(String videoFilename, String thumbFilename, int width,int height, int hour, int min, float sec) throws IOException,InterruptedException{
        videoThumbTaker.getThumb(videoFilename,thumbFilename,width,height,hour,min,sec);
    }

    /**
     * 获取第一帧的视频图片
     * @param videoFilename 视频文件路径
     * @param thumbFilename 截图文件路径
     * @param width         图片宽度
     * @param height        图片高度
     * @throws IOException
     * @throws InterruptedException
     */
    public void getFirstThumb(String videoFilename, String thumbFilename, int width,int height) throws IOException, InterruptedException {
        videoFirstThumbTaker.getThumb(videoFilename, thumbFilename, width, height, 0, 0, 1);
    }

    /**
     * 获取最后一帧的视频图片
     * @param videoFilename 视频文件路径
     * @param thumbFilename 截图文件路径
     * @param width 图片宽度
     * @param height 图片高度
     * @throws IOException
     * @throws InterruptedException
     */
    public void getLastThumb(String videoFilename, String thumbFilename, int width,int height) throws IOException, InterruptedException {
        VideoInfoService videoInfoService = new VideoInfoService(ffmpegApp);
        VideoInfo videoInfo = videoInfoService.getInfo(videoFilename);
        videoLastThumbTaker.getThumb(videoFilename, thumbFilename, width, height, videoInfo.getHours(), videoInfo.getMinutes(), videoInfo.getSeconds() - 0.2f);
    }

    public String getFfmpegApp() {
        return ffmpegApp;
    }

    public void setFfmpegApp(String ffmpegApp) {
        this.ffmpegApp = ffmpegApp;
    }
}
